
package com.shop.cereshop.admin.controller.wallet;

import com.shop.cereshop.admin.annotation.NoRepeatSubmit;
import com.shop.cereshop.admin.annotation.NoRepeatWebLog;
import com.shop.cereshop.admin.param.wallet.WalletParam;
import com.shop.cereshop.admin.param.wallet.AdminModifyPayPasswordParam;
import com.shop.cereshop.admin.service.wallet.BuyerWalletService;
import com.shop.cereshop.admin.service.wallet.WalletTransactionService;
import com.shop.cereshop.admin.service.user.CerePlatformUserRoleService;
import com.shop.cereshop.admin.page.role.PlatformUserRole;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import javax.annotation.Resource;


/**
 * <p>
 * 前端控制器
 * 用户钱包表
 * </p>
 *
 * @author wang.yao
 * @date 2025-08-26
 */
@Slf4j
@RestController
@RequestMapping("/buyerWallet")
@Api(value = "BuyerWallet", tags = "用户钱包模块")
public class BuyerWalletController {

    @Resource
    private WalletTransactionService walletTransactionService;

    @Resource
    private BuyerWalletService buyerWalletService;

    @Resource
    private CerePlatformUserRoleService cerePlatformUserRoleService;

    /**
     * 用户钱包冻结(冻结置顶金额和时间)
     */
    @PostMapping("/wallet/freezed")
    @ApiOperation(value = "用户钱包冻结/解封")
    @NoRepeatSubmit
    @NoRepeatWebLog
    public Result<Void> freezedWallet(@RequestBody WalletParam param) throws CoBusinessException {
        walletTransactionService.freezedWallet(param);
        return Result.success();
    }

    /**
     * 管理员修改用户支付密码
     */
    @PostMapping("/payPassword/adminModify")
    @ApiOperation(value = "管理员修改用户支付密码")
    public Result adminModifyPayPassword(@RequestBody AdminModifyPayPasswordParam param, HttpServletRequest request) {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        if (user == null) {
            return Result.unauthorized();
        }
        // 基于角色表校验是否为管理员角色
        List<PlatformUserRole> roles = cerePlatformUserRoleService.findRolesByUserId(user.getPlatformUserId());
        boolean isAdminRole = roles != null && roles.stream().anyMatch(r -> "管理员".equals(r.getRoleName()));
        if (!isAdminRole) {
            return Result.forbidden();
        }
        // 基础格式校验
        if (!param.isPayPasswordValidFormat()) {
            return Result.custom(CoReturnFormat.PARAM_INVALID, "支付密码格式错误，应为6位数字");
        }
        if (!param.isConfirmPayPasswordValidFormat()) {
            return Result.custom(CoReturnFormat.PARAM_INVALID, "确认支付密码格式错误，应为6位数字");
        }
        if (!param.isPasswordMatch()) {
            return Result.custom(CoReturnFormat.PAY_PASSWORD_CONFIRM_ERROR, "两次输入的支付密码不一致");
        }
        buyerWalletService.adminModifyPayPassword(param.getBuyerUserId(), param.decryptPayPassword(),request);
        return Result.success();
    }

}
